package com.gdk.ksoft.assembly.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.entry.tables.CustomerAreaTable;
import com.gdk.ksoft.data.entry.tables.CustomerTypeTable;
import com.gdk.ksoft.ui.customers.models.AddrInfoModel;
import com.gdk.ksoft.assembly.utils.system.SQLiteUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by Mrper on 2014/11/22.
 * 退出提示框
 */
public class CustomerAddrEditDialog extends Dialog {

    public interface OnValueChangedListener{
        public void onValueChanged(AddrInfoModel info);
    }

    @ViewInject(R.id.dialog_customerAddrEdit_txtTitle)private TextView txtTitle;//标题文字
    @ViewInject(R.id.dialog_customerAddrEdit_etAddress)private EditText etAddress;//收货地址
    @ViewInject(R.id.dialog_customerAddrEdit_etAbbreviation)private EditText etAbbr;//地址简称
    @ViewInject(R.id.dialog_customerAddrEdit_etCustomerConnectity)private EditText etConnectity;//联系方式
    @ViewInject(R.id.dialog_customerAddrEdit_txtCustomerType)private TextView txtCustomerType;//客户类型
    @ViewInject(R.id.dialog_customerAddrEdit_etContractorName)private EditText etContractor;//客户联系人
    @ViewInject(R.id.dialog_customerAddrEdit_txtCustomerArea)private TextView txtCustomerArea;//地址区域
    @ViewInject(R.id.dialog_customerAddrEdit_etManagementArea)private EditText etManagerArea;//经营面积
    @ViewInject(R.id.dialog_customerAddrEdit_txtOK)private TextView txtOk;//确认操作
    @ViewInject(R.id.dialog_customerAddrEdit_txtCancel)private TextView txtCancel;//取消操作

    private AddrInfoModel addrInfo;//编辑的客户地址信息
    private OnValueChangedListener listener;//植发生改变的监听器
    private DbUtils dbUtils;//数据库操作对象
    /**
     * 构造函数
     * @param context
     * @param title 窗口标题
     */
    public CustomerAddrEditDialog(Context context,String title) {
        super(context);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View rootView = View.inflate(context, R.layout.dialog_customerinfo_edit, null);
        setContentView(rootView);
        ViewUtils.inject(this, rootView);
        dbUtils  = SQLiteUtils.getInstance(context).getDbUtils();
        addrInfo = new AddrInfoModel();
        txtTitle.setText(title);
        init(context);//初始化操作
    }

    /**
     * 构造函数
     * @param context
     * @param title 窗口标题
     * @param data 窗口内容数据
     */
    public CustomerAddrEditDialog(Context context,String title,AddrInfoModel data) {
        super(context);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View rootView = View.inflate(context, R.layout.dialog_customerinfo_edit, null);
        setContentView(rootView);
        ViewUtils.inject(this, rootView);
        txtTitle.setText(title);
        if(data!=null){//初始化界面参数
            etAddress.setText(data.addr);
            etAbbr.setText(data.abbr);
            txtCustomerType.setText(data.ctype);
            txtCustomerArea.setText(data.carea);
            etContractor.setText(data.contractor);
            etConnectity.setText(data.connectity);
            etManagerArea.setText(data.addrArea);
            this.addrInfo = data;//获取地址数据参数
        }
        init(context);//初始化操作
    }

    /**
     * 初始化操作
     * @param context
     */
    public void init(final Context context){
        setOnButtonClickListener();//设置按钮的点击事件
        new AsyncTask<Void,Void,Void>(){
            //客户类型数组
            private String[] ctypeArr = null;
            private int[] intCtypeArr = null;
            //客户地区数组
            private String[] careaArr = null;
            private int[] intCareaArr = null;
            @Override
            protected Void doInBackground(Void... params) {
                try{
                    List<CustomerTypeTable> ctypelist = dbUtils.findAll(CustomerTypeTable.class);
                    if(ctypelist!=null && ctypelist.size()>0){
                        ctypeArr = new String[ctypelist.size()];
                        intCtypeArr = new int[ctypeArr.length];
                        for(int i=0;i<ctypelist.size();i++){
                            CustomerTypeTable item = ctypelist.get(i);
                            ctypeArr[i] = item.khlx;
                            intCtypeArr[i] = item.id;
                        }
                    }
                    List<CustomerAreaTable> carealist = dbUtils.findAll(CustomerAreaTable.class);
                    if(carealist!=null && carealist.size()>0){
                        careaArr = new String[carealist.size()];
                        intCareaArr = new int[careaArr.length];
                        for(int i=0;i<carealist.size();i++){
                            CustomerAreaTable item = carealist.get(i);
                            careaArr[i] = item.mc;
                            intCareaArr[i] = item.id;
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                dealResult();//处理查询结果
            }

            /**
             * 处理查询结果
             */
            public void dealResult(){
                if(ctypeArr!=null && ctypeArr.length>0) {
                    if(addrInfo.intCtype == 0){
                        addrInfo.intCtype = intCtypeArr[0];
                        addrInfo.ctype = ctypeArr[0];
                        txtCustomerType.setText(ctypeArr[0]);
                    }
                    txtCustomerType.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            new AlertDialog.Builder(context).setItems(ctypeArr, new OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    txtCustomerType.setText(ctypeArr[which]);
                                    addrInfo.intCtype = intCtypeArr[which];
                                    addrInfo.ctype = ctypeArr[which];
                                }
                            }).show();
                        }
                    });
                }
                if(careaArr!=null && careaArr.length>0) {
                    if(addrInfo.intCarea == 0){
                        addrInfo.intCarea = intCareaArr[0];
                        addrInfo.carea = careaArr[0];
                        txtCustomerArea.setText(careaArr[0]);
                    }
                    txtCustomerArea.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            new AlertDialog.Builder(context).setItems(careaArr, new OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    txtCustomerArea.setText(careaArr[which]);
                                    addrInfo.intCarea = intCareaArr[which];
                                    addrInfo.carea = careaArr[which];
                                }
                            }).show();
                        }
                    });
                }
            }
        }.execute();
    }

    /**
     * 设置数据变化的监听事件
     * @param listener
     */
    public void setOnValueChangedListener(OnValueChangedListener listener){
        this.listener = listener;
    }

    /**
     * 设置按钮的点击事件
     */
    private void setOnButtonClickListener(){
        txtOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!checkArguments()) return;
                //获取当前输入的参数
                addrInfo.addr = etAbbr.getText().toString();
                addrInfo.abbr = etAbbr.getText().toString();
                addrInfo.contractor = etContractor.getText().toString();
                addrInfo.connectity = etConnectity.getText().toString();
                addrInfo.addrArea = etManagerArea.getText().toString();
                if(listener!=null)
                    listener.onValueChanged(addrInfo);
                CustomerAddrEditDialog.this.dismiss();
            }
        });
        txtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomerAddrEditDialog.this.dismiss();

            }
        });
    }

    /**
     * 检查参数的正确性
     * @return
     */
    public boolean checkArguments(){
        if(etAddress.getText().length()<=0){
            etAddress.setError("请输入收货地址!");
            return false;
        }
        if(etAbbr.getText().length()<=0){
            etAbbr.setError("请输入地址简称!");
            return false;
        }
        if(etContractor.getText().length()<=0){
            etContractor.setError("请输入收货人!");
            return false;
        }
        if(etConnectity.getText().length()<=0){
            etConnectity.setError("请输入联系方式!");
            return false;
        }
        return true;
    }

}
